<style>
    .layui-colla-title {
        background-color: #5FB878;

    }
/*    #tips from,#tips{width: 100%; height: 100%}
    #tips_btn{
        position: absolute;
        left: 35%;
        bottom: 0;
        margin:  5px 2%;
        text-align: center;
        z-index: 99999;
    }*/
</style><!-- -->
<form class="layui-form" action="{:url('planSave', ['mid' => $mid])}"  method="post" id="editForm">
	<h2 class="layui-colla-title {if $omInfo['caichuang_status']!=3}layui-colla-title-warn{/if}">面料名称：{$omInfo['mInfo']['name']}<span class="ml10">[用量：{$omInfo['customer_dosage']}]</span>
        <span>[实际单件用量：{$omInfo['caichuang_shiji_dosage']}]</span>
    </h2>
	<fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
	  <legend>唛架信息</legend>
	</fieldset>

	<table class="layui-table">
		<thead>
			<tr>
				<th>订单数量</th>
				<th><span>{$oInfo['quantity']}</span> 件</th>
				<th>高层件数</th>
				<th><span class="height-count-total">
				{php}
					$num = 0;
					foreach($osList as $k => $v){
						$num += arrayGet($v['wheat_height_count_detail'], $mid, 0);
					}
					echo $num;
				{/php}
				</span>件</th>
				<th>低层件数</th>
				<th><span class="low-count-total">
				{php}
					$num = 0;
					foreach($osList as $k => $v){
						$num += arrayGet($v['wheat_low_count_detail'], $mid, 0);
					}
					echo $num;
				{/php}
				</span>件</th>
			</tr>
		</thead>
		<tbody>
			{foreach $osList as $k => $v}
			<tr>
				<td>{$v['size_name']}</td>
				<td>{$v['order_count']}   {if isset($skuStockList[$v['size_name']])}<span style="text-indent:35px;color:red;"> &nbsp;&nbsp;&nbsp;可用库存：<strong>{$skuStockList[$v['size_name']]}</strong></span>{/if}</td>
				<td>{$v['size_name']}</td>
				<td><input type="text" name="wheat_height_count[{$v['mid']}]" value="{:arrayGet($v['wheat_height_count_detail'], $mid, 0)}" tabindex="100" lay-verify="required|mimoint" autocomplete="off" placeholder="" class="layui-input height-count"></td>
				<td>{$v['size_name']}</td>
				<td><input type="text" name="wheat_low_count[{$v['mid']}]" value="{:arrayGet($v['wheat_low_count_detail'], $mid, 0)}" tabindex="200" lay-verify="required|mimoint" autocomplete="off" placeholder="" class="layui-input low-count"></td>
			</tr>
			{/foreach}
			<tr>
				<td>捆条单件用量</td>
				<td><input type="text" name="caichuang_strip_dosage" value="{if $omInfo['caichuang_status']>0}{$omInfo['caichuang_strip_dosage']}{/if}" tabindex="300" lay-verify="required|number" autocomplete="off" placeholder="" class="layui-input strip-dosage"></td>
				<td>唛架总米长(高层)</td>
				<td><input type="text" name="caichuang_wheat_height_length" value="{if $omInfo['caichuang_status']>0}{$omInfo['caichuang_wheat_height_length']}{/if}" tabindex="500" lay-verify="required|number" autocomplete="off" placeholder="" class="layui-input"></td>
				<td>唛架总米长(低层)</td>
				<td><input type="text" name="caichuang_wheat_low_length" value="{if $omInfo['caichuang_status']>0}{$omInfo['caichuang_wheat_low_length']}{/if}" tabindex="400" lay-verify="required|number" autocomplete="off" placeholder="" class="layui-input"></td>
			</tr>
            <tr>
            	<td>捆条宽度(米)</td>
                <td><input type="text" name="caichuang_strip_width" value="{$omInfo['caichuang_strip_width']}" autocomplete="off" lay-verify="mimonumber" class="layui-input"></td>
                <td>实裁布封</td>
                <td><input type="text" name="caichuang_fabric_width" value="{$omInfo['caichuang_fabric_width']}" autocomplete="off" lay-verify="mimonumber" class="layui-input"></td>
                <td>料率百分比(<br />如:83.86%则填83.76)</td>
                <td><input type="text" name="material_per" value="{$omInfo['material_per']}" autocomplete="off" lay-verify="required|mimonumber" class="layui-input"></td>
            </tr>
		</tbody>
	</table>

	<fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
	  <legend>面料计划数据</legend>
      
	</fieldset>
	<table class="layui-table">
		<thead>
			<tr>
				<th>布号</th>
                <th>开单采购总数</th>
				<th>米数(米)</th>
				<th>次品数</th>
				<th>计划拉布层数(高层)</th>
				<th>计划拉布层数(低层)</th>
				<th></th>
				<th></th>
			</tr>
		</thead>
		<tbody>
			{foreach $omiList as $k => $v}
			<tr>
				<td>{$v['cloth_num_full']}</td>
                <td></td>
				<td>{$v['length']}</td>
				<td>
					{if $v['rejects_length']>0 }
					<span style="color:red;">{$v['rejects_length']}</span>
					{/if}
				</td>
				<td><input type="text" name="caichuang_wheat_height[{$v['id']}]" value="{$v['caichuang_wheat_height']}" lay-verify="required|mimoint" autocomplete="off" placeholder="" class="layui-input height-num"></td>
				<td><input type="text" name="caichuang_wheat_low[{$v['id']}]" value="{$v['caichuang_wheat_low']}" lay-verify="required|mimoint" autocomplete="off" placeholder="" class="layui-input low-num"></td>
				<td><a href="{:url('purchasingV2/returnClothnum')}?mid={$omInfo.mid}&from=caichuang" title="退料" class="layui-btn layui-btn-sm layui-btn-normal hisi-iframe" hisi-data="{width: '800px', height: '500px', endRefresh:'layer'}">退料</a></td>
				<td></td>
			</tr>
			{/foreach}
			
			{if !empty($mscInfo) }
			<tr>
				<td>库存面料</td>
                <td></td>
				<td>{$mscInfo['length']}</td>
				<td></td>
				<td><input type="text" name="msc[{$mscInfo['id']}][caichuang_wheat_height]" value="{$mscInfo['caichuang_wheat_height']}" lay-verify="required|mimoint" autocomplete="off" placeholder="" class="layui-input height-num"></td>
				<td><input type="text" name="msc[{$mscInfo['id']}][caichuang_wheat_low]" value="{$mscInfo['caichuang_wheat_low']}" lay-verify="required|mimoint" autocomplete="off" placeholder="" class="layui-input low-num"></td>
				<td></td>
				<td></td>
			</tr>
			{/if}
			
			<tr class="layui-bg-gray">
				<td>合计：{:count($omiList)+(!empty($mscInfo)?1:0)}条</td>
                <td>{:$omInfo['billing_caigou_count']}</td>
				<td>{:array_sum(array_column($omiList, 'length'))+arrayGet($mscInfo, 'length', 0)}</td>
				<td>
					{if array_sum(array_column($omiList, 'rejects_length'))>0 }
					<span style="color:red;">{:array_sum(array_column($omiList, 'rejects_length'))}</span>
					{/if}
				</td>
				<td class="height-num-total">{:array_sum(array_column($omiList, 'caichuang_wheat_height'))+arrayGet($mscInfo, 'caichuang_wheat_height', 0)}</td>
				<td class="low-num-total">{:array_sum(array_column($omiList, 'caichuang_wheat_low'))+arrayGet($mscInfo, 'caichuang_wheat_low', 0)}</td>
				<td>预留捆条：<span class="strip-dosage-total">{:$omInfo['caichuang_strip_dosage']*$omInfo['yingcai_count']*1.1}</span>米</td>
				<td>应裁数：<span class="yingcai-count">{$omInfo['yingcai_count']}</span>件</td>
			</tr>
		</tbody>
	</table>
    <div id="tips" style="display:none">
        <div style="display:none">
            <input type="text" id="dif1" name="dif1" value="0">
            <input type="text" id="dif2" name="dif2" value="0">
        </div>
        {foreach $overfeedReasonKV as $k => $v}
        <input type="radio" name="overfeed_reason" value="{$k}" title="{$v}" {if $k == 3 }checked{/if}>
        {/foreach}
    </div>
	{if $omInfo['caichuang_status'] !=3 }
	<div class="pop-bottom-bar">
    	<input type="hidden" name="yingcai_count" value="{$omInfo['yingcai_count']}">
        <a class="layui-btn layui-btn-normal" lay-submit="" id="replaceSubmit" lay-filter="check">提交审核</a>
     
        <button  type="submit" class="layui-btn layui-btn-normal" id="formSubmit" lay-submit="formSubmit" hisi-data="{pop: true, refresh: false, callback: callbackPlan}" lay-filter="formSubmit" style="display:none" >提交审核</button>
        <a href="javascript:parent.layui.layer.closeAll();" class="layui-btn layui-btn-primary ml10">取消</a>
    </div>
    {/if}
</form>

{include file="system@block/layui" /}

<script type="text/javascript">
    layui.use(['table', 'jquery', 'layer', 'form', 'tool'], function() {
    	var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;

        var regFloat = /((^[1-9]\d*)|^0)(\.\d{0,2}){0,1}$/; // 浮点数，小数点后两位
		var regInt = /^\d+$/; // 整数
        form.verify({
        	mimonumber: function(value, item){
        	    if (value=='') return '';
        	    if(! regFloat.test(value)){
        	    	return '只能填写数字';
        	    }
        	},
        	mimoint: function(value, item){
        	    if (value=='') return '';
        	    if(! regInt.test(value)){
        	    	return '只能填写数字';
        	    }
        	}
        });
        
        form.on('submit(check)', function (data) {       
            var formData = data.field,k,caichuangWheatLowNumber=0,caichuangWheatHeightNumber=0,caichuangShijiDosage=0,customerDosage={$omInfo['customer_dosage']},materialDosage={$omInfo['material_dosage']};
            var caichuangStripDosage=Number(formData.caichuang_strip_dosage),
            caichuangWheatLowLength=Number(formData.caichuang_wheat_low_length),
            caichuangWheatHeightLength=Number(formData.caichuang_wheat_height_length);
            for (k in formData){
                if(k.indexOf("wheat_low_count") != -1){
                    caichuangWheatLowNumber+=Number(formData[k]);
                }
                if(k.indexOf("wheat_height_count") != -1){
                    caichuangWheatHeightNumber+=Number(formData[k]);
                } 
            }
            
            console.log(formData);
            //console.log(caichuangWheatLowLength+caichuangWheatHeightLength);
            //console.log(caichuangWheatLowNumber+caichuangWheatHeightNumber);
            if(caichuangWheatLowNumber+caichuangWheatHeightNumber > 0) {
            	
            	var low=caichuangWheatLowNumber!=0?(caichuangWheatLowLength/caichuangWheatLowNumber):0;
				var hight=caichuangWheatHeightNumber!=0?(caichuangWheatHeightLength/caichuangWheatHeightNumber):0;
				var num=2;
				if(low==0 || hight==0) num=1;
				caichuangShijiDosage = Number((low+hight)/num);
    		}
            var ShijiDosage=Number(caichuangShijiDosage)+Number(caichuangStripDosage);
            var dif1=ShijiDosage-customerDosage;
            var dif2=ShijiDosage-materialDosage;
            
            console.log('超客户用量：'+ShijiDosage+'-'+customerDosage+'='+dif1);           
            console.log('超工厂用量：'+ShijiDosage+'-'+materialDosage+'='+dif2);           
            if(dif1 > 0.03 || dif2 > 0.05){
                $("#dif1").val(dif1);
                $("#dif2").val(dif2);
                
                // 超工厂用量，默认选中第4个
                if(dif2 > 0.05) $("input:radio[value=4]").attr('checked', 'true');
                form.render('radio');
                
                layer.open({
                    type: 1,
                    title:'多料原因',
                    skin: 'layui-layer-rim', //加上边框
                    area: ['220px', '240px'], //宽高
                    btn: ['确定'],
                    content: $('#tips'),
                    btn1: function(index, layero){
                        $("#replaceSubmit").removeClass('layui-btn-normal').addClass('layui-btn-disabled').prop('disabled', true).text('提交中...');
                        $("#formSubmit").click();  
                    },
                    success: function(layero, index){
                    	$("input[name='overfeed_reason']:radio:last").attr('checked', 'true');
                    }
                });  
            }else{
                $(this).removeClass('layui-btn-normal').addClass('layui-btn-disabled').prop('disabled', true).text('提交中...');
                $("#formSubmit").click();               
            }
            
             
        });
		// 实时统计应裁数
		window.yingcaiStat = function() {
			// 应裁数=（拉布层数（低层）总数 X 唛架低层总件数） + （拉布层数（低层）总数 X 唛架低层总件数）
			var low = layui.tool.bcmul($('.low-count-total').html(), $('.low-num-total').html(), 0);
			var height = layui.tool.bcmul($('.height-count-total').html(), $('.height-num-total').html(), 0);
			var total = layui.tool.bcadd(low, height, 0);
			$('input[name="yingcai_count"]').val(total);
			$('.yingcai-count').html(total);
			var caichuangStripDosage = $('.strip-dosage').val();
			$('.strip-dosage-total').html(layui.tool.bcmul(caichuangStripDosage*total, 1.1));
		}
		
		// 低层件数
        $(document).on('change','input.low-count',function(){
        	var total = 0;
        	$("input.low-count").each(function(i){
        		total = layui.tool.bcadd(total, this.value, 0);
        	});
        	$('.low-count-total').html(total);
        	yingcaiStat();
        });
		// 高层件数
        $(document).on('change','input.height-count',function(){
        	var total = 0;
        	$("input.height-count").each(function(i){
        		total = layui.tool.bcadd(total, this.value, 0);
        	});
        	$('.height-count-total').html(total);
        	yingcaiStat();
        });
     	// 低层数
        $(document).on('change','input.low-num',function(){
        	var total = 0;
        	$("input.low-num").each(function(i){
        		total = layui.tool.bcadd(total, this.value, 0);
        	});
        	$('.low-num-total').html(total);
        	yingcaiStat();
        });
		// 高层数
        $(document).on('change','input.height-num',function(){
        	var total = 0;
        	$("input.height-num").each(function(i){
        		total = layui.tool.bcadd(total, this.value, 0);
        	});
        	$('.height-num-total').html(total);
        	yingcaiStat();
        });
		// 捆条单件用量
        $(document).on('change','input.strip-dosage',function(){
        	yingcaiStat();
        });
		
		// 面料计划录入回调
		window.callbackPlan = function(obj, res){
			if (res.code == 1) {              
                $("#replaceSubmit").addClass('layui-btn-normal').removeClass('layui-btn-disabled').prop('disabled', true).text(res.msg);
				setTimeout(function(){
					var index = parent.layer.getFrameIndex(window.name);
					parent.layer.close(index);
				}, 2000);
				parent.callbackPlan(obj, res);
			}
		};
    });
</script>